package com.smu.recognize.faceCompare;

import com.arcsoft.face.*;
import com.arcsoft.face.enums.DetectMode;
import com.arcsoft.face.enums.DetectOrient;
import com.arcsoft.face.enums.ErrorInfo;
import lombok.extern.slf4j.Slf4j;

@Slf4j
public class FeatureCompare {

    public FaceSimilar compare(FaceFeature faceFeature1,FaceFeature faceFeature2){

        //从官网获取
        String appId = "Axr3vzVwi1MGZ7i1bZYqKLJrn84xaAhYHB3gWRYwCgHa";
        String sdkKey = "6GwQrZmt4EQkWqpPdbNMTZMpjrr8yHMfyyZtSj7RHhzQ";
        //引入动态库ls
        FaceEngine faceEngine = new FaceEngine("/arcface/lib");
        log.info("查找文件");
        //激活引擎
        int errorCode = faceEngine.activeOnline(appId, sdkKey);

        if (errorCode != ErrorInfo.MOK.getValue() && errorCode != ErrorInfo.MERR_ASF_ALREADY_ACTIVATED.getValue()) {
            System.out.println("引擎激活失败");
        }
        ActiveFileInfo activeFileInfo=new ActiveFileInfo();
        errorCode = faceEngine.getActiveFileInfo(activeFileInfo);
        if (errorCode != ErrorInfo.MOK.getValue() && errorCode != ErrorInfo.MERR_ASF_ALREADY_ACTIVATED.getValue()) {
            System.out.println("获取激活文件信息失败");
        }
        //引擎配置
        EngineConfiguration engineConfiguration = new EngineConfiguration();
        engineConfiguration.setDetectMode(DetectMode.ASF_DETECT_MODE_IMAGE);
        engineConfiguration.setDetectFaceOrientPriority(DetectOrient.ASF_OP_ALL_OUT);
        engineConfiguration.setDetectFaceMaxNum(1);
        engineConfiguration.setDetectFaceScaleVal(16);
        //功能配置
        FunctionConfiguration functionConfiguration = new FunctionConfiguration();
        functionConfiguration.setSupportFace3dAngle(true);
        functionConfiguration.setSupportFaceDetect(true);
        functionConfiguration.setSupportFaceRecognition(true);
        engineConfiguration.setFunctionConfiguration(functionConfiguration);
        //初始化引擎
        errorCode = faceEngine.init(engineConfiguration);
        if (errorCode != ErrorInfo.MOK.getValue()) {
            System.out.println("初始化引擎失败");
        }
        //人脸比对
        FaceSimilar faceSimilar=new FaceSimilar();
        errorCode=faceEngine.compareFaceFeature(faceFeature1,faceFeature2,faceSimilar);

        System.out.println("相似度"+faceSimilar.getScore());
        if (faceEngine!=null){
                faceEngine.unInit();
        }
        return faceSimilar;
    }
}
